package com.cn.xiaonuo.modular.controller.app;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.modular.model.app.VideoDeploy;
import com.cn.xiaonuo.modular.model.app.VideoStore;
import com.cn.xiaonuo.modular.service.app.VideoDeployAndStoreService;
/**
 * 配置视频和存储视频
 *
 * 
 * 
 */
@Controller
@RequestMapping("/video")
public class VideoDeployAndStoreController {
	 @Resource
	 private VideoDeployAndStoreService videoDeployAndStoreService;

	 private final ResourceLoader resourceLoader;
	  @Autowired
	  public VideoDeployAndStoreController(ResourceLoader resourceLoader) {
	    this.resourceLoader = resourceLoader;
	  }
	  @ResponseBody
	    @PostMapping("/findVideoDeploy")
	    public ResponseData updateUserInfo() {
		  VideoDeploy videoDeploy=videoDeployAndStoreService.fingVideoDeploy();
	    	ResponseData r=new ResponseData();
	    	r.setData(videoDeploy);
	    	r.setSuccess(true);
	        return r;
	    }
	  
	  @ResponseBody
	    @PostMapping("/insertOrUpdateVideoDeploy")
	    public ResponseData insertOrUpdateVideoDeploy(VideoDeploy videoDeploy) {
		  videoDeployAndStoreService.insertOrUpdateVideoDeploy(videoDeploy);
	        return ResponseData.success();
	    }
	  @RequestMapping(value = "/uploadVideo", method={RequestMethod.POST,RequestMethod.GET})
	    @ResponseBody
	    public ResponseData uploadVideo(VideoStore videoStore,MultipartFile video,MultipartFile screenshot,HttpServletRequest request) {
		  videoDeployAndStoreService.videoStore(videoStore, video,screenshot, request,"/upload/");
		  return  ResponseData.success();
	  }
	  @RequestMapping(value = "/findVideo", method={RequestMethod.POST,RequestMethod.GET})
	    @ResponseBody
	    public ResponseData findVideo(VideoStore videoStore,HttpServletRequest request) {
			ResponseData r=new ResponseData();
			r.setData(videoDeployAndStoreService.findVideoStore(videoStore));
	    	r.setSuccess(true);
	    	return  r;
	  }
	  @RequestMapping("show")
	  public ResponseEntity showPhotos(String fileName){

	    try {
	      // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
	      return ResponseEntity.ok(resourceLoader.getResource("file:" + "/upload/" + fileName));
	    } catch (Exception e) {
	      return ResponseEntity.notFound().build();
	    }
	  }
	  @RequestMapping(value ="/getFileSrc" ,method = RequestMethod.GET)
	  @ResponseBody
	  public void getFileSrc(HttpServletRequest request ,HttpServletResponse response,@RequestParam(value="path") String path) {
	        File file = new File("/upload/"+path);// 视频路径
	        if(!file.exists()){
	            throw new RuntimeException("视频文件不存在！");
	        }
	        byte[] fba = getFileByteArr(file);

	        OutputStream sos = null;
	        try {
	            response.setHeader("Content-Type", "video/mp4");
	            sos = response.getOutputStream();
	            sos.write(fba);
	            sos.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }

	    }

	    private byte[] getFileByteArr (File file){
	        byte[] buffer = null;
	        try{
	            FileInputStream fis = new FileInputStream(file);
	            ByteArrayOutputStream bos = new ByteArrayOutputStream();
	            byte[] b = new byte[1024];
	            int n;
	            while ((n = fis.read(b)) != -1)
	            {
	                bos.write(b, 0, n);
	            }
	            fis.close();
	            bos.close();
	            buffer = bos.toByteArray();
	        }catch (FileNotFoundException e){
	            e.printStackTrace();
	        }catch (IOException e){
	            e.printStackTrace();
	        }
	        return buffer;
	    }
}
